﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace MVCCentral.Framework.Mvc.Fyneworks
{
    class RaterBuilder : FormBuilder
    {
        private string id;
        private double? Avg;
        private int? Votes;
        private string CurrentlyRated;

        public RaterBuilder(string id, double? avg, int? votes, string currentlyRated)
            : base("StarsRater")
        {
            this.id = id;
            this.Avg = avg ?? 0;
            this.Votes = votes;
            this.CurrentlyRated = currentlyRated;
        }

        /// <summary>
        /// Create form to be used for rating.
        /// </summary>
        public override void OpenForm()
        {
            Form.OpenForm = "<form action=\"\" method=\"post\" id=\"" + Form.FormName + "\">" + Environment.NewLine;
        }

        public override void GenerateInputs()
        {
            TagBuilder tag = null;
            string[] titles = new string[] { "Poor", "Fair", "Good", "Great", "Excellent" };

            for (int i = 1; i <= 5; i++)
            {
                tag = new TagBuilder("input");
                tag.MergeAttribute("type", "radio");
                tag.MergeAttribute("name", "score");
                tag.MergeAttribute("class", "auto-submit-star");
                tag.MergeAttribute("value", i.ToString());
                tag.MergeAttribute("title", titles[i - 1]);
                Form.Tags.Add(tag.ToString() + Environment.NewLine);
            }

            tag = new TagBuilder("input");
            tag.MergeAttribute("type", "hidden");
            tag.MergeAttribute("name", "Id");
            tag.MergeAttribute("id", "Id");
            tag.MergeAttribute("value", id);
            Form.Tags.Add(tag.ToString() + Environment.NewLine);
        }

        public override void EndForm()
        {
            string endform = "</form>\n<span id=\"stars-result\" style=\"margin:0 0 0 10px;\">" + Environment.NewLine;
            if (Votes != null && Avg != null)
            {
                double average = 0.0d;
                if (Avg > 0)
                {
                    average = Math.Round((double)Avg, 2);
                }
                
                endform += string.Format(CurrentlyRated, average, Votes);
            }
            endform += "</span>\n";

            Form.EndForm = endform;
        }

        public override void OpenDiv()
        {
            string openDiv = string.Empty;
            openDiv = "<span id=\"rater\">" + Environment.NewLine;
            //openDiv = "<div style=\"float:left;\">" + Environment.NewLine;
            Form.OpenDiv = openDiv;
        }

        public override void CloseDiv()
        {
            string div = string.Empty;
            //div = "</div>" + Environment.NewLine;
            div = "</span>" + Environment.NewLine + "<div style=\"clear:both\"></div>" + Environment.NewLine;

            Form.CloseDiv = div;
        }
    }
}
